class Group:

    def __init__(self, intervalLeft, intervalRight, countArray=None, simiSumArray=None):
        self.intervalLeft = intervalLeft
        self.intervalRight = intervalRight
        if countArray is None:
            self.countArray = [0 for i in range(10000)]
        else:
            self.countArray = countArray
        if simiSumArray is None:
            self.simiSumArray = [0 for i in range(10000)]
        else:
            self.simiSumArray = simiSumArray

    def __str__(self):
        """
        Examples
        --------
        group = Group("A", "B")
        group.simiDict["5"]["count"] += 1
        print(group)
        A:B~[0,0,...,1,...,0]-[0,0,...,0,...,0]
        """
        return "%s:%s~%s-%s" % (self.intervalLeft, self.intervalRight, str(self.countArray), str(self.simiSumArray))

    def get_count_sum(self):
        count = 0
        for i in self.countArray:
            count += i
        return count

    def get_global_mean(self):
        globalSum = 0
        for simiSum in self.simiSumArray:
            globalSum += simiSum
        return 0 if self.get_count_sum() == 0 else globalSum / self.get_count_sum()

    def get_percentile(self, percentile):
        assert 0 <= percentile <= 100
        countSum = self.get_count_sum()
        # print(countSum)
        index = (percentile / 100 * countSum)
        # print(index)
        if index - int(index) <= 0.0001:  # index为整数
            i = self.__get_index(int(index))
            count = self.countArray[i]
            return 0 if count == 0 else self.simiSumArray[i] / self.countArray[i]
        else:  # index为小数
            i1 = self.__get_index(int(index))
            i2 = self.__get_index(int(index + 1))
            count = self.countArray[i1] + self.countArray[i2]
            return 0 if count == 0 else (self.simiSumArray[i1] + self.simiSumArray[i2]) / count

    def get_min(self):
        for i in range(len(self.countArray)):
            if self.countArray[i] != 0:
                return self.simiSumArray[i] / self.countArray[i]

    def get_max(self):
        res = 0
        for i in range(len(self.countArray))[::-1]:
            if self.countArray[i] != 0:
                return self.simiSumArray[i] / self.countArray[i]

    def __get_index(self, index):
        count = 0
        for i in range(len(self.countArray)):
            count += self.countArray
            if count >= index:
                return int(i)
